package com.atguigu.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


import com.atguigu.common.constant.WareConstant;
import com.atguigu.gulimall.ware.vo.MergeVo;
import com.atguigu.gulimall.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.ware.entity.PurchaseEntity;
import com.atguigu.gulimall.ware.service.PurchaseService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 采购信息
 *
 * @author lemon
 * @email 2313812755@qq.com
 * @date 2022-11-20 10:45:59
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    // 完成采购单 /ware/purchase/done
    @PostMapping("done")
    public R purchaseDone(@RequestBody PurchaseDoneVo purchaseDoneVo) {

        purchaseService.purchaseDone(purchaseDoneVo);

        return R.ok();
    }



    /**
     * 领取采购单  /ware/purchase/received
     * @param ids 表示我们采购单的id
     * @return
     */
    @PostMapping("/received")
    public R purchaseReceived(@RequestBody List<Long> ids) {

        purchaseService.purchaseReceived(ids);

        return R.ok();
    }

    // 合并采购需求  /ware/purchase/merge
    /**
     * 请求参数
     * {
     *   purchaseId: 1, //整单id
     *   items:[1,2,3,4] //合并项集合
     * }
     * @param mergeVo
     * @return
     */
    @PostMapping("/merge")
    //@RequiresPermissions("ware:purchase:list")
    public R purchaseMerge(@RequestBody MergeVo mergeVo){

        // 注意这里是有两种情况的 如果我们没有选择合并整单 没有选择 那么会自动新建一个purshI
        // 因为我们先要根据purchaseId先查询看是否存在了
        String s = purchaseService.purchaseMerge(mergeVo);

        if("chongfu".equalsIgnoreCase(s)) {
            //
            return R.error(WareConstant.MergerPurchaseEnum.ERROR.getCode(), WareConstant.MergerPurchaseEnum.ERROR.getMsg());
        }
        // 合并成功就返回ok
        return R.ok();
    }

    /**
     *
     * @param params
     * @return
     */
    // /ware/purchase/unreceive/list  查询未领取的采购单
    @GetMapping("/unreceive/list")
    //@RequiresPermissions("ware:purchase:list")
    public R unreceiveList(@RequestParam Map<String, Object> params){

        PageUtils page = purchaseService.unreceiveList(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:purchase:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase){

        // 设置默认时间
        purchase.setUpdateTime(new Date());
        purchase.setCreateTime(new Date());
		purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
